/**
 * 直播配置js
 */
$(function(){
	
	$('#courtid').combobox({
		 required:true,
		 method:'get',
		 editable:false,
		 url:$Path+'/admin/court/getCombobox',
		 valueField:'id',
		 textField:'text',
		 panelHeight: "auto",
		 onLoadSuccess:function(data){
			 $('#courtid').combobox('select',data[0].id);
		 },
		 onSelect:function(value){
			 if(!jQuery.isEmptyObject("#openCourt")){
				 $("#openCourt").val(value.text); 
			 }
			 $("#opencourt").val(value.text);
			 $.ajax({
				 type:'get',
				 data:{courtid:value.id},
				 url:$Path+'/admin/micro/getMicroByCourt',
				 success:function(data){
					 if(data.code==200){
						 var temp="";
						 $.each(data.results, function (i, n) {
							 if(n.staffrole == undefined || n.staffrole == null || n.staffrole == ""){
								 temp+='<div class="col-xs-12" style="margin-top:20px">'+
									'<span class="word" style="width:90px">'+n.microname+'</span>'+
									'<input type="text" style="width:200px" class="form-control" dataatr="'+n.microid+'" value="'+n.staffrole+'" placeholder="'+n.microname+'角色">'+
								'</div>';
							 }else{
								 temp+='<div class="col-xs-12" style="margin-top:20px">'+
									'<span class="word" style="width:90px">'+n.microname+'</span>'+
									'<input type="text" style="width:200px" class="form-control" dataatr="'+n.microid+'" value="'+n.staffrole+'" placeholder="'+n.microname+'角色">'+
								'</div>';
							 }
						 });
						 $(".microlist").html(temp);
					 }
					 else{
						 parent.$.messager.alert('失败',data.msg,'error');
					 }
				 }
			 })
		 }
	});
	var codelength=$('#casecode').width();
	var date=new Date();
	$("#openDate").datebox({
		required:true,
		editable:false,
		panelWidth:codelength+18
	});;
	$("#openDate").datebox('setValue',Common.formatterDate(date));
	$("#secrets").combobox({
		 required:true,
		 method:'get',
		 editable:false,
		 url:$Path+"/admin/secret/getUserCombobox",
		 valueField:'id',
		 textField:'text',
		 panelHeight: "auto",
		 onLoadSuccess:function(data){
			 $("#secrets").combobox('select',data[0].id);
		 },
	 })
	 $('#casecode').validatebox({
	    required: true,
	    tipPosition:'top',
	    deltaX:codelength/4
	 });
	 $('#casename').validatebox({
		    required: true,
		    tipPosition:'top',
		    deltaX:codelength/4
	 });
	 
	 $("#isPublic").combobox({
		 required:true,
		 method:'get',
		 editable:false,
		 url:$Path+"/admin/dict/getCombobox?dictCode=DR_CASE_ISPUBLIC",
		 valueField:'id',
		 textField:'text',
		 panelHeight: "auto",
		 onLoadSuccess:function(data){
			 $("#isPublic").combobox('select',data[0].id);
		 },
	 })
	$('#casesort').combobox({
		 required:true,
		 method:'get',
		 editable:false,
		 url:$Path+'/admin/casesort/getCombobox',
		 valueField:'id',
		 textField:'text',
		 panelHeight: "auto",
		 onLoadSuccess:function(data){
			 $('#casesort').combobox('select',data[0].id);
		 },
		 onSelect:function(value){
			 $.ajax({
				 method:'get',
				 data:{sortid:value.id},
				 url:$Path+'/admin/casesort/getTempletBySortId',
				 success:function(data){
					 if(data.code==200){
						 //生成模板代码
						 var temp=jQuery.parseJSON(data.results);
						 $(".caselist").html('');
						 if(temp!=null){
							 for(var i=0;i<temp.length;i++){
								 if(temp[i].configSort=='text'){
									 $(".caselist").append(' <div class="col-xs-12" style="margin-top:20px">'+
												'<span class="word">'+temp[i].configName+'</span>'+
												'<input type="text" class="form-control" typeattr="'+temp[i].configSort+'" nameattr="'+temp[i].configName+'"'+
												'id="'+temp[i].configId+'" placeholder="'+temp[i].configName+'">'+
											'</div>');
								 }
								 if(temp[i].configSort=='date'){
									 $(".caselist").append('<div class="col-xs-12" style="margin-top:20px">'+
												'<span class="word">'+temp[i].configName+'</span>'+
												'<input type="text" class="form-control" typeattr="'+temp[i].configSort+'" nameattr="'+temp[i].configName+'"'+
												'id="'+temp[i].configId+'" placeholder="'+temp[i].configName+'" style="width:43%">'+
											'</div>');
									 //添加js
									 $("#jsScript").append(renderHtml(temp[i].configId,temp[i].isInit,'',temp[i].configSort));
								 }
								 if(temp[i].configSort=='combobox'){
									 $(".caselist").append('<div class="col-xs-12" style="margin-top:20px" >'+
												'<span class="word">'+temp[i].configName+'</span>'+
												'<input type="text" class="form-control" typeattr="'+temp[i].configSort+'" nameattr="'+temp[i].configName+'"'+ 
												'id="'+temp[i].configId+'" style="width:30%">'+
											'</div>');
									 $("#jsScript").append(renderHtml(temp[i].configId,temp[i].isInit,temp[i].dictcode,temp[i].configSort));
								 }
							 }
						 }
					 }
					 else{
						 parent.$.messager.alert('失败',data.msg,'error');
					 }
				 }
			 })
		 }
	});
	var date=new Date();
	$('#opendate').datebox({
	});
	$('#opendate').datebox('setValue',Common.formatterDate(date));
	
	
})
/**
 * 添加脚本
 * @param id 元素id
 * @param isinit 是否初始化
 * @param dictCode 字典表code
 * @param type 类型
 */
function renderHtml(id,isinit,dictCode,type){
	if(type=='date'){
		var scprit='$("#'+id+'").datebox({required:true});';
		if(isinit){
			scprit+='var date=new Date();';
			scprit+='$("#'+id+'").datebox(\'setValue\',Common.formatterDate(date));';
		}
		return '<script type="text/javascript">'+scprit+'</script>';
	}
	else if(type=='combobox'){
		var shijian='';
		if(isinit){
			shijian+='onLoadSuccess:function(data){'+
				 '$("#'+id+'").combobox(\'select\',data[0].id);'+
			 '},';
		}
		var scprit='$("#'+id+'").combobox({'+
			 'required:true,'+
			 'method:\'get\','+$Path+
			 'url:"/admin/dict/getCombobox?dictCode='+dictCode+'",'+
			 'valueField:\'id\','+
			 'textField:\'text\','+shijian+
		'})';
		return '<script type="text/javascript">'+scprit+'</script>';
	}
	else{
		return '';
	}
}
function onSubmit(){
	var cases={};
	cases.courtid=$("#courtid").combobox('getValue');
	cases.sortid=$("#casesort").combobox('getValue');
	cases.casecode=$("#casecode").val();
	cases.casename=$("#casename").val();
	cases.opendate=$("#openDate").datebox('getValue');
	cases.opencourt=$("#opencourt").val();
	cases.judgerchief=$("#judgerchief").val();
	cases.judger=$("#judger").val();
	cases.clerker=$("#clerker").val();
	cases.ispublic=$("#isPublic").combobox('getValue');
	cases.secrets=$("#secrets").combobox('getValue');
	cases.videourl=$("#videourl").val();
	
	
	var today=Common.formatterDate(new Date());
	if(cases.opendate<today){
		parent.$.messager.alert('警告',"开庭时间不可小于当前日期",'warning');
		return false;
	}
	/**
	 * 获取麦克风列表
	 */
	var micros=new Array();
	var isright=true;
	var preNode;
	$(".microlist input[type='text']").each(function(){
		var micro={};
		micro.microid=this.attributes.dataatr.value;
		micro.staffrole=this.value;
		if(micro.staffrole.length>50){
			preNode=this.previousSibling.innerText;
			isright=false;
			return false;
		}
		micros.push(micro);
	});
	if(!isright){
		parent.$.messager.alert('警告',preNode+"长度超出限制",'warning');
		return false;
	}
	/**
	 * 获取案件配置列表
	 */
	var configs=new Array();
	$(".caselist input[type='text']").each(function(){
		var config={};
		config.configcode=this.id;
		if(config.configcode!=""&&config.configcode!=undefined){
			config.configname=this.attributes.nameattr.value;
			var type=this.attributes.typeattr.value;
			if(type=='text'){
				config.value=this.value;
			}
			else if(type=='date'){
				config.value=$("#"+this.id).datebox('getValue');
			}
			else if(type=='combobox'){
				config.value=$("#"+this.id).combobox('getText');
			}
			else{
				config.value='';
			}
			configs.push(config);
		}
	});
	if(micros==null){
		parent.$.messager.alert('失败',"麦克风列表不可为空",'error');
		return;
	}
	if($.trim(cases.casename)==null||$.trim(cases.casename)==''||$.trim(cases.casename)==undefined){
		parent.$.messager.alert('失败',"案件名称不可为空",'error');
		return;
	}
	if($.trim(cases.casename).length>50){
		parent.$.messager.alert('失败',"案件名称超出长度限制",'error');
		return;
	}
	if($.trim(cases.casecode)==null||$.trim(cases.casecode)==''||$.trim(cases.casecode)==undefined){
		parent.$.messager.alert('失败',"案件编号不可为空",'error');
		return;
	}
	if($.trim(cases.casecode).length>50){
		parent.$.messager.alert('失败',"案件编号超出长度限制",'error');
		return;
	}
	if($.trim(cases.opendate)==null||$.trim(cases.opendate)==''||$.trim(cases.opendate)==undefined){
		parent.$.messager.alert('失败',"开庭时间不可为空",'error');
		return;
	}
	if($.trim(cases.ispublic)==null||$.trim(cases.ispublic)==''||$.trim(cases.ispublic)==undefined){
		parent.$.messager.alert('失败',"是否公开不可为空",'error');
		return;
	}
	if($.trim(cases.secrets)==null||$.trim(cases.secrets)==''||$.trim(cases.secrets)==undefined){
		parent.$.messager.alert('失败',"密级不可为空",'error');
		return;
	}
	var auth=window.parent.getAuth();
	$.ajax({
			type:'POST',
			url:$Path+'/admin/case/saveCase',
			data:{configs:configs,micros:micros,cases:cases,authenticityToken:auth},
			success:function(data){
				if(data.code==200){
					window.parent.opentab('02001','关键词优化','/admin/keywords_major','caseid='+data.results.caseid+'&courtid='+data.results.courtid);
				}
				else{
					parent.$.messager.alert('失败',data.msg,'error');
				}
			}
		})

//	window.parent.opentab('02001','关键词优化','/admin/keywords_major','configs='+configs+'&micros='+micros+'&cases='+cases);
}